package com.example.rental_system.service.imp;

import cn.hutool.core.date.DateTime;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.rental_system.mapper.HouseMapper;
import com.example.rental_system.pojo.House;
import com.example.rental_system.service.HouseService;
import com.example.rental_system.utils.ConstantOssPropertiesUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.UUID;

@Service
public class HouseServiceImp extends ServiceImpl<HouseMapper, House> implements HouseService {

    @Autowired
    private HouseMapper houseMapper;
    @Override
    public IPage<House> queryByUserId(int userId, Integer current, Integer size) {
        Page<House> page = new Page<>(current,size);

        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",0);
        return houseMapper.selectPage(page,queryWrapper);
    }

    @Override
    public String upload(MultipartFile file) {
        //地域节点
        String endpoint= ConstantOssPropertiesUtils.EDNPOINT;
        String accessKeyId = ConstantOssPropertiesUtils.ACCESS_KEY_ID;
        String secret = ConstantOssPropertiesUtils.SECRECT;
        // bucket
        String bucketName = ConstantOssPropertiesUtils.BUCKET;

        try {
            // 创建ossClient
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, secret);
            // 上传文件流
            InputStream inputStream = file.getInputStream();
            String fileName = file.getOriginalFilename();
            //相同文件会被覆盖
            // 生成随机唯一id  uuid   添加到文件名中    图片名称加上 随机id
            String uuid = UUID.randomUUID().toString().replaceAll("-","");
            fileName = uuid + fileName;
            //按照日期  创建文件夹
            // 2023.2.22/01.jpg
            String timeUrl = new DateTime().toString("yyyy/MM/dd");
            fileName = timeUrl +"/"+ fileName;
            // 调用方法实现上传
            ossClient.putObject(bucketName,fileName,inputStream);
            // 关闭ossClient
            ossClient.shutdown();
            // 上传文件之后 文件路径
            String url = "https://"+bucketName+"."+endpoint+"/"+fileName;
            return url;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public IPage<House> queryAll(int i, int i1) {
        Page<House> housePage=new Page<>(i,i1);
        QueryWrapper<House> houseQueryWrapper=new QueryWrapper<>();
        Page<House> housePage1 = houseMapper.selectPage(housePage, houseQueryWrapper);
        return housePage1;
    }

    @Override
    public String updateHouse(String img, MultipartFile file) {

        //地域节点
        String endpoint= ConstantOssPropertiesUtils.EDNPOINT;
        String accessKeyId = ConstantOssPropertiesUtils.ACCESS_KEY_ID;
        String secret = ConstantOssPropertiesUtils.SECRECT;
        // bucket
        String bucketName = ConstantOssPropertiesUtils.BUCKET;

        try {
            // 创建ossClient
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, secret);
            // 上传文件流
            InputStream inputStream = file.getInputStream();
            // 上传文件之后 文件路径
            String http = "https://"+bucketName+"."+endpoint+"/";
            String url = img.replaceAll(http, "");
            System.out.println(url);
            // 调用方法实现上传
            ossClient.putObject(bucketName,url,inputStream);
            // 关闭ossClient
            ossClient.shutdown();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public IPage<House> queryHouseByUserId(String userName,Integer start,Integer size) {
        Page<House> page=new Page<>(start,size);
        QueryWrapper<House> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userName);
        return houseMapper.selectPage(page,queryWrapper);

    }

    @Override
    public IPage<House> queryHousesByUserId(String userName, int start, int size) {

        Page<House> page=new Page<>(start,size);
        QueryWrapper<House> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("publisher",userName);
        return houseMapper.selectPage(page,queryWrapper);
    }
}
